<?php

namespace handle\message\lib\factory;

use handle\message\lib\driver\EventDriver;
use handle\message\lib\Entrance;
use handle\message\lib\globals\System;
use handle\message\lib\manage\ChessBoardManage;
use handle\message\lib\manage\PlayerManage;
use handle\message\lib\manage\RoomManage;
use handle\message\lib\manage\strategy\ChessBoard;
use handle\message\lib\traits\ManageSingleton;

class SingletonFactory
{
    use ManageSingleton;
    /**
     * 单例实例组
     * @var array
     */
    private static array $instances = [];

    /**
     * 获取单例实例
     * @param string $instanceName
     * @param bool $newInstance     是否创建新的实例
     * @return ChessBoardManage|EventDriver|PlayerManage|RoomManage|System|null
     */
    public function makeInstances(string $instanceName, bool $newInstance = false){
        ($newInstance or (!isset(self::$instances[$instanceName]) or empty(self::$instances[$instanceName]))) and self::$instances[$instanceName] = self::createInstance($instanceName);    //如果不存在实例或者需要重新创建实例
        return self::$instances[$instanceName];
    }

    /**
     * 获取单例实例
     * @param string $instanceName
     * @return ChessBoardManage|EventDriver|Entrance|ChessBoard|PlayerManage|RoomManage|SingletonFactory|null
     */
    private static function createInstance(string $instanceName){
        switch ($instanceName){
            case "room":
                return RoomManage::getInstance();
            case "player":
                return PlayerManage::getInstance();
            case "board":
                return ChessBoardManage::getInstance();
            case "event":
                return EventDriver::getInstance();
            case 'system':
                return System::getInstance();
            default:
                return null;
        }
    }
}